<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>DijkstraAllPairsSP.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="DijkstraAllPairsSP code in Java">
<meta NAME="TITLE" CONTENT="DijkstraAllPairsSP code in Java">
<meta NAME="KEYWORDS" CONTENT="DijkstraAllPairsSP,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>DijkstraAllPairsSP.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "DijkstraAllPairsSP.java">DijkstraAllPairsSP.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac DijkstraAllPairsSP.java</span>
<span class="comment"> *  Execution:    none</span>
<span class="comment"> *  Dependencies: EdgeWeightedDigraph.java Dijkstra.java</span>
<span class="comment"> *</span>
<span class="comment"> *  Dijkstra's algorithm run from each vertex. </span>
<span class="comment"> *  Takes time proportional to E V log V and space proportional to EV.</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">DijkstraAllPairsSP</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class represents a data type for solving the</span>
<span class="comment"> *  all-pairs shortest paths problem in edge-weighted digraphs</span>
<span class="comment"> *  where the edge weights are nonnegative.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  This implementation runs Dijkstra's algorithm from each vertex.</span>
<span class="comment"> *  The constructor takes time proportional to </span><span class="keyword">&lt;em&gt;</span><span class="comment">V</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> (</span><span class="keyword">&lt;em&gt;</span><span class="comment">E</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> log </span><span class="keyword">&lt;em&gt;</span><span class="comment">V</span><span class="keyword">&lt;/em&gt;</span><span class="comment">)</span>
<span class="comment"> *  and uses space proprtional to </span><span class="keyword">&lt;em&gt;</span><span class="comment">V</span><span class="keyword">&lt;/em&gt;&lt;sup&gt;</span><span class="comment">2</span><span class="keyword">&lt;/sup&gt;</span><span class="comment">,</span>
<span class="comment"> *  where </span><span class="keyword">&lt;em&gt;</span><span class="comment">V</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is the number of vertices and </span><span class="keyword">&lt;em&gt;</span><span class="comment">E</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is the number of edges.</span>
<span class="comment"> *  Afterwards, the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">dist()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">hasPath()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> methods take</span>
<span class="comment"> *  constant time and the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">path()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> method takes time proportional to the</span>
<span class="comment"> *  number of edges in the shortest path returned.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation,    </span>
<span class="comment"> *  see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://algs4.cs.princeton.edu/44sp"</span><span class="keyword">&gt;</span><span class="comment">Section 4.4</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of    </span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Algorithms, 4th Edition</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> by Robert Sedgewick and Kevin Wayne. </span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">DijkstraAllPairsSP</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> DijkstraSP</span><span class="symbol">[]</span><span class="normal"> all</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Computes a shortest paths tree from each vertex to to every other vertex in</span>
<span class="comment">     * the edge-weighted digraph </span><span class="keyword">&lt;tt&gt;</span><span class="comment">G</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> G the edge-weighted digraph</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if an edge weight is negative</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException unless 0 </span><span class="preproc">&amp;le;</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> </span><span class="preproc">&amp;le;</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">V</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> - 1</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">DijkstraAllPairsSP</span><span class="symbol">(</span><span class="usertype">EdgeWeightedDigraph</span><span class="normal"> G</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        all  </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> DijkstraSP</span><span class="symbol">[</span><span class="normal">G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">()];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span>
<span class="normal">            all</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">DijkstraSP</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">,</span><span class="normal"> v</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a shortest path from vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> to vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">t</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> s the source vertex</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> t the destination vertex</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> a shortest path from vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> to vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">t</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     *    as an iterable of edges, and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">null</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if no such path</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Iterable&lt;DirectedEdge&gt;</span><span class="normal"> </span><span class="function">path</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> s</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> t</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> all</span><span class="symbol">[</span><span class="normal">s</span><span class="symbol">].</span><span class="function">pathTo</span><span class="symbol">(</span><span class="normal">t</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Is there a path from the vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> to vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">t</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">?</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> s the source vertex</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> t the destination vertex</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if there is a path from vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> </span>
<span class="comment">     *    to vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">t</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">hasPath</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> s</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> t</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="function">dist</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">,</span><span class="normal"> t</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&lt;</span><span class="normal"> Double</span><span class="symbol">.</span><span class="normal">POSITIVE_INFINITY</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the length of a shortest path from vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> to vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">t</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> s the source vertex</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> t the destination vertex</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the length of a shortest path from vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> to vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">t</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">;</span>
<span class="comment">     *    </span><span class="keyword">&lt;tt&gt;</span><span class="comment">Double.POSITIVE_INFINITY</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if no such path</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">dist</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> s</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> t</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> all</span><span class="symbol">[</span><span class="normal">s</span><span class="symbol">].</span><span class="function">distTo</span><span class="symbol">(</span><span class="normal">t</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>
<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
